/**
 * @title 响应结构体
 */
export interface IResponse<T = any> {
    code: number
    message: string
    data: T
}

/**
 * @title 使用Promise 包装的响应体解构
 * @example Promise 
    {
        code: number
        message: string
        data:any
    }
 */
export type IRes<T = any> = Promise<IResponse<T>>

/**
* @title 带有分页结构的响应体
* @example 
Promise 
    {
        code: number
        message: string
        data:{
            data:T
            page:number
            total_records:string
        }
    }
*/
export type IResWithPage<T = any> = Promise<
    IResponse<
        {
            data: T
        } & IPagination
    >
>

export type IPagination = {
    total_records: any
    page: number
    limit: number
    total_count: number
    total_page: number
}

export type IPageQuery = Pick<IPagination,'page'|'limit'>

// 定义一个工具类型来推断 data 属性的类型
export type UnwrapData<T> = T extends Promise<infer U>
    ? U extends IResponse<infer R>
        ? R
        : never
    : never

/**
 * @description 定义一个工具类型来推断 data.data 数组的第一个元素的类型
 */
export type UnwrapResponse<T> = T extends Promise<infer U>
    ? U extends IResponse<infer R>
        ? R extends { data: infer D1 }
            ? D1 extends Array<infer D1Child>
                ? D1Child
                : UnwrapData<D1>
            : R extends Array<infer D2Child>
            ? D2Child
            : R
        : never
    : never

/**
 * @description 定义一个工具类型来推断一个函数的返回值的data类型
 * @example type IDatasetItem = UnwrapRes<typeof LifeApi.getSohFactorDataset>
 */
export type UnwrapRes<T extends (...args: any[]) => any> = UnwrapResponse<
    ReturnType<T>
>

// 定义一个工具类型来推断函数参数的类型
export type UnwrapParams<T extends (...args: any[]) => any> = T extends (
    ...args: infer P
) => any
    ? P
    : never

export type UnwrapParamsFirst<T extends (...args: any[]) => any> = T extends (
    ...args: infer P
) => any
    ? P[0]
    : never

// NOTE: 获取接口请求函数的参数类型的示例
export type UnwrapResData<T extends (...args: any) => any> = Awaited<ReturnType<T>>['data']

// 移动端可能使用的类型别名
export type ApiResponse<T = any> = IResponse<T>
export type PaginationResponse<T = any> = {
    data: T
} & IPagination
